package com.foreverwin.mes.common.service.impl;


import com.baomidou.mybatisplus.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.common.mapper.KeyLockMapper;
import com.foreverwin.mes.common.model.KeyLock;
import com.foreverwin.mes.common.service.KeyLockService;
import com.foreverwin.mes.core.base.FrontPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author syngna123
 * @since 2018-11-15
 */
@Service
@Transactional
public class KeyLockServiceImpl extends ServiceImpl<KeyLockMapper, KeyLock> implements KeyLockService {


    @Autowired
    private KeyLockMapper keyLockMapper;

    @Override
    public Page<KeyLock> selectPage(FrontPage<KeyLock> frontPage) {
        EntityWrapper<KeyLock> entityWrapper = new EntityWrapper<KeyLock>();
        //getEntityWrapper(entityWrapper, keyLock);
        return super.selectPage(frontPage.getPagePlus(), entityWrapper);
    }

    @Override
    public List<KeyLock> selectList() {
        EntityWrapper<KeyLock> entityWrapper = new EntityWrapper<KeyLock>();
        //getEntityWrapper(entityWrapper, keyLock);
        return super.selectList(entityWrapper);
    }

    @Override
    public boolean insert(KeyLock keyLock) {
        try {
            return super.insert(keyLock);
        } catch(Exception e) {
            if( e instanceof SQLIntegrityConstraintViolationException || e instanceof DuplicateKeyException) {
                throw new MybatisPlusException("重复键值 '" + keyLock.getHandle() + "'");
            } else {
                 throw e;
            }
        }
    }

    @Override
    public int batchDeleteByRefKey(List<String> objRefs) {
        return keyLockMapper.batchDeleteByRefKey(objRefs);
    }
}